#! /bin/bash

screen -d -m /usr/bin/agent

shutdown() {
    echo "Exiting..."
    pkill -x agent
    pkill -x nmap
    while pgrep -x nmap > /dev/null || pgrep -x agent > /dev/null ; do
	sleep 5
	echo "Killing..."
	pkill -9 -x nmap
	pkill -9 -x agent
    done
    # kill backgrounded log()
    kill %1
}

trap shutdown TERM

log() {
    while true ; do
	echo "$(date): $(find input/ -type f | wc -l) targets waiting"
	sleep 60
    done
}

log &
wait
